package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.GoodsCompose;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class GoodsDaoImpl extends TempDao {

	public Map<Integer, GoodsInfo> getGoodsInfoMap() {
		Map<Integer, GoodsInfo> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_goods";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsInfo data = new GoodsInfo();
				data.setGoodsId(rs.getInt("id"));
				data.setGoodsName(rs.getString("name"));
				data.setType(rs.getInt("type"));
				data.setQuality(rs.getInt("quality"));
				data.setCanUse(rs.getInt("canUse"));
				data.setTopLimit(rs.getInt("maxNum"));
				data.setSort(rs.getInt("sort"));
				data.setParamStr(rs.getString("params"));
				data.setParamList(StringUtils.stringToBigIntegerList(data.getParamStr(), "\\|"));
				data.setExtendParam(rs.getString("extendParam"));
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, Map<Integer, List<GoodsBagInfo>>> getGoodsBagInfoMap() {
		Map<Integer, Map<Integer, List<GoodsBagInfo>>> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_goods_bag";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsBagInfo data = new GoodsBagInfo();
				data.setId(rs.getInt("id"));
				data.setBagId(rs.getInt("boxId"));
				data.setGoodsId(rs.getInt("itemId"));
				data.setMinNum(rs.getInt("minNum"));
				data.setMaxNum(rs.getInt("maxNum"));
				data.setRandomType(rs.getInt("randomType"));
				data.setRandomWeight(rs.getInt("probability"));
				data.setNotice(rs.getInt("isNotice"));
				if(!dataMap.containsKey(data.getBagId())){
					dataMap.put(data.getBagId(), new ConcurrentHashMap<>());
				}
				if(!dataMap.get(data.getBagId()).containsKey(data.getRandomType())){
					dataMap.get(data.getBagId()).put(data.getRandomType(), new ArrayList<>());
				}
				dataMap.get(data.getBagId()).get(data.getRandomType()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, GoodsCompose> getGoodsComposeMap() {
		Map<Integer, GoodsCompose> dataMap = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_goods_compose";
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsCompose data = new GoodsCompose();
				data.setGoodsId(rs.getInt("composition"));
				data.setNotice(rs.getBoolean("isNotice"));
				data.setConsume(PropertyHelper.parseStringToProperty(rs.getString("materials")));
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}